package com.example.demo.controller;

import com.example.demo.entity.Application;
import com.example.demo.entity.Response;
import com.example.demo.entity.Student;
import com.example.demo.service.Impl.ApplicationImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static java.time.LocalDateTime.now;
import static java.util.Map.of;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;

@RestController
@RequestMapping("/application")
@RequiredArgsConstructor
public class ApplicationController {
    private final ApplicationImpl applicationImpl;

    @GetMapping("/getApplication/{studentId}")
    public ResponseEntity<Response> getApplication(@PathVariable Long studentId) {
        try{
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("getApplication", applicationImpl.findAllApplication(studentId)))
                            .message("查找成功")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("查找失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
    @GetMapping("/getAllApplication")
    public ResponseEntity<Response> getAllApplication() {
        try{
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("getAllApplication", applicationImpl.findAllApplication()))
                            .message("查找成功")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("查找失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
    @GetMapping("/getAllApplicationByID/{studentId}")
    public ResponseEntity<Response> getAllApplication(@PathVariable("studentId") Long studentId) {
        try{
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("getAllApplication", applicationImpl.findApplication(studentId)))
                            .message("查找成功")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("查找失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
    @PostMapping("/addApplication")
    public ResponseEntity<Response> addApplication(@RequestBody Application application,
                                               @RequestParam("studentAccountId") Long studentAccountId){
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("addApplication",applicationImpl.addApplication(application, studentAccountId)))
                            .message("新增报名表")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            //如果出现异常，打印堆栈信息，并返回一个失败的响应对象
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("新增报名表失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
    @DeleteMapping("/deleteApplication/{applicationId}")
    public ResponseEntity<Response> deleteApplication(@PathVariable("applicationId") Long applicationId){
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("deleteApplication",applicationImpl.deleteApplication(applicationId)))
                            .message("删除报名表")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            //如果出现异常，打印堆栈信息，并返回一个失败的响应对象
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("删除报名表失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }
    @PostMapping("/updateApplication")
    public ResponseEntity<Response> updateApplication(@RequestBody Application application){
        try {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("updateApplication",applicationImpl.updateApplication(application)))
                            .message("修改报名表")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        } catch (Exception e) {
            //如果出现异常，打印堆栈信息，并返回一个失败的响应对象
            e.printStackTrace();
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(null)
                            .message("修改报名表失败")
                            .status(BAD_REQUEST)
                            .statusCode(BAD_REQUEST.value())
                            .build()
            );
        }
    }

}
